﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using DailyDiet.Models;
using DailyDiet.Models.VO;
using DailyDiet.Services;
using DailyDiet.Services.Navigation;
using DailyDiet.Views;
using DailyDiet.Views.SubPage;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using SQLite;
using Xamarin.Forms;

namespace DailyDiet.ViewModels
{
    /// <summary>
    /// 推荐详情页面ViewModel
    /// </summary>
    public class RecDietsDetailPageViewModel : ViewModelBase {

        /******** 构造函数 ********/

        /// <summary>
        /// 导航服务
        /// </summary>
        private IContentNavigationService _navigationService;

        /// <summary>
        /// 菜品存储服务
        /// </summary>
        private IDietStorageService _dietStorageService;

        /// <summary>
        /// 菜品记录存储
        /// </summary>
        private IDietRecorderStorageService _dietRecorderStorageService;

        /// <summary>
        /// 警告服务
        /// </summary>
        private IAlertService _alertService;

        public RecDietsDetailPageViewModel(IContentNavigationService contentNavigationService, IDietStorageService dietStorageService, IDietRecorderStorageService dietRecorderStorageService,IAlertService alertService)
        {
            _navigationService = contentNavigationService;
            _dietStorageService = dietStorageService;
            _dietRecorderStorageService = dietRecorderStorageService;
            _alertService = alertService;
            _customMenu = new ObservableCollection<Diet>();

            //用于测试
            //初始化上一个页面传递的参数RecDietViewObject
            if (RecDietViewObject == null) {
                _recDietViewObject = new RecDietViewObject();
            }
        }


        /******** 绑定属性 ********/

        /// <summary>
        /// RecDietViewObject-->RecPage传入的参数
        /// </summary>
        private RecDietViewObject _recDietViewObject;
        public RecDietViewObject RecDietViewObject
        {
            get => _recDietViewObject;
            set
            {
                Set(nameof(RecDietViewObject), ref _recDietViewObject, value);
            }
        }

        /// <summary>
        /// 页面下部分的自定义菜单
        /// </summary>
        public ObservableCollection<Diet> CustomMenu {

            get => _customMenu;
            set => Set(nameof(CustomMenu), ref _customMenu, value);
        }

        private ObservableCollection<Diet> _customMenu;
        /// <summary>
        /// 关键词--->传入SelfDefinePage
        /// </summary>
        public string SearchKey {

            get => _searchKey;
            set => Set(nameof(SearchKey), ref _searchKey, value);
        }

        private string _searchKey;

        //私有变量
        private Shell _shell;
        public Shell Shell =>
            _shell ?? (_shell = Shell.Current );

        /******** 绑定命令 ********/


        /// <summary>
        /// 菜品搜索命令--->将搜索关键字传入SelfDefinePage页面
        /// </summary>
        private RelayCommand _searchCommand;
        public RelayCommand SearchCommand =>
            _searchCommand ?? (_searchCommand =
                new RelayCommand(async () => await SearchCommandFunc(SearchKey)));
        public async Task SearchCommandFunc(string key)
        {
            await _navigationService.NavigateToAsync(
                ContentNavigationServiceConstants.SelfDefinePage, key);
        }


        /// <summary>
        /// 确定命令
        /// </summary>
        private RelayCommand _confirmCommand;
        public RelayCommand ConfirmCommand =>
            _confirmCommand ?? (_confirmCommand =
                new RelayCommand(async () => await ConfirmCommandFunc()));
        public async Task ConfirmCommandFunc() {

            //剥离的确定命令中的可测试部分insertRecorder
            InsertRecorder();
             await Shell.Navigation.PopToRootAsync();
             _alertService.ShowAlert("提示","菜单确定成功","确定");
            CustomMenu.Clear();
        }

        /// <summary>
        /// 剥离的确定命令中的可测试函数insertRecorder
        /// </summary>
        public async void  InsertRecorder() {
            string dateString = DateTime.Now.ToString("yyyy-MM-dd");
            List<DietRecorder> recorders = new List<DietRecorder>();
            for (int i = 0; i < CustomMenu.Count; i++)
            {
                Diet diet = CustomMenu[i];
                DietRecorder dietRecorder = new DietRecorder();
                dietRecorder.Date = dateString;
                dietRecorder.MealTimes = RecDietViewObject.MealTimes;
                dietRecorder.DietId = diet.Id;
                dietRecorder.Id = null;
                recorders.Add(dietRecorder);
            }

            await _dietRecorderStorageService.InsertAll(recorders);
        }

        /// <summary>
        /// 跳转至反馈界面Command
        /// </summary>
        private RelayCommand _goToFeedbackCommand;
        public RelayCommand GoToFeedbackCommand =>
            _goToFeedbackCommand ?? (_goToFeedbackCommand =
                new RelayCommand(async () => await GoToFeedbackCommandFunc()));
        public async Task GoToFeedbackCommandFunc() {
            await _navigationService.NavigateToAsync(ContentNavigationServiceConstants.DietFeedbackPage);
        }

        /// <summary>
        /// 增加命令
        /// </summary>
        private RelayCommand<Diet> _addCommand;
        public RelayCommand<Diet> AddCommand =>
            _addCommand ?? (_addCommand =
                new RelayCommand<Diet>(async diet => await AddCommandFunc(diet)));
        public async Task AddCommandFunc(Diet diet) {
            if (CustomMenu.Contains(diet)) {
                _alertService.ShowAlert("提示", "此菜品已经添加完成", "确定");

            } else {
                
                //剥离的可测试部分
                AddRecordToCustomMenu(diet);
                _alertService.ShowAlert("提示", "菜品添加成功", "确定");
                if (Shell.CurrentPage.Title == "自定义页面")
                {
                    await Shell.CurrentPage.Navigation.PopAsync();
                }

            }
        }

        private RelayCommand<Diet> _showDetailCommand;

        public RelayCommand<Diet> ShowDetailCommand =>
            _showDetailCommand ?? (_showDetailCommand =
                new RelayCommand<Diet>(async (Diet diet) => ShowDetailCommandFunc(diet)));

        public async Task ShowDetailCommandFunc(Diet diet) {
            await _navigationService.NavigateToAsync(
                ContentNavigationServiceConstants.DietsDetailPage, diet);
        }

        /// <summary>
        ///剥离的增加命令中的可测试函数addRecordToCustomMenu
        /// </summary>
        /// <param name="diet"></param>
        public void AddRecordToCustomMenu(Diet diet) {
            if (!CustomMenu.Contains(diet)) {
                CustomMenu.Add(diet);
            }
        }


        /// <summary>
        /// 删除命令
        /// </summary>
        private RelayCommand<Diet> _deleteCommand;
        public RelayCommand<Diet> DeleteCommand =>
            _deleteCommand ?? (_deleteCommand =
                new RelayCommand<Diet>(async diet => await DeleteCommandFunc(diet)));
        public async Task DeleteCommandFunc(Diet diet) {

            //剥离的可测试函数
            DeleteRecordFromCustomMenu(diet);
            _alertService.ShowAlert("提示", "菜品删除成功", "确定");
        }

        /// <summary>
        /// 删除命令中剥离的可测试函数 deleteRecordFromCustomMenu
        /// </summary>
        /// <param name="diet"></param>
        public void DeleteRecordFromCustomMenu(Diet diet) {
            CustomMenu.Remove(diet);
        }
    }
}
